import path from 'path';
import { defineConfig, loadEnv } from 'vite';
import createVitePlugins from './vite/plugins';

// https://cn.vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  const { VITE_SUB_DOMAIN, VITE_APP_BASE_API } = env;

  return {
    base: VITE_SUB_DOMAIN,
    plugins: createVitePlugins(env, command === 'build'),
    resolve: {
      alias: {
        '~': path.resolve(__dirname, './'),
        '@': path.resolve(__dirname, './src'),
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue'],
    },
    server: {
      https: true,
      port: 9908,
      host: true,
      open: false,
      proxy: {
        [VITE_APP_BASE_API]: {
          target: 'http://localhost:8090',
          changeOrigin: true,
        },
      },
    },
    //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              },
            },
          },
          require('tailwindcss'),
        ],
      },
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler', // 使用现代编译器 API
          silenceDeprecations: ['legacy-js-api'], // 暂时静默弃用警告
        },
      },
    },
  };
});
